﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product.RateFactorConverter
{
    public class BasicAmountConverter:AbstractRateFactorConverter
    {
        private static readonly string[] alias = new string[] { "基本保额", "基础保额", "基本保费", "基本保费", "阶梯保额","份数","份" };

        private static readonly string amtTemplate = @"(\d*)([百千万]?)元?";

        private static readonly string qtyTemplate = @"(\d*)份?";

        public override string[] Alias
        {
            get { return alias; }
        }

        private static IBasicAmount CreateProperty(string item)
        {
            Match m = null;

            IBasicAmount ba = new BasicAmount();

            m=Regex.Match(item, amtTemplate);

            if (m.Success)
            {
                
                string s = m.Groups[2].Value;
                int v = int.Parse(m.Groups[1].Value);

                if (string.IsNullOrEmpty(s))
                    ba.Basic = v;
                else if (s == "百")
                    ba.Basic = v * 100;
                else if (s == "千")
                    ba.Basic = v * 1000;
                else if (s == "万")
                    ba.Basic = v * 10000;

                ba.Min = ba.Basic;//默认最小金额等于基本金额
                ba.Name = item;

                return ba;
            }

            m = Regex.Match(item, qtyTemplate);

            if (m.Success)
            {
                int v = int.Parse(m.Groups[1].Value);

                ba.Basic = v;
                ba.Min = v;
                ba.Name = item;

                return ba;
            }

            throw new Exception(string.Format("无法将[{0}]转换成基本保额!", item));

        }

        public static void CreateProperties(IProduct product, string[] items)
        {
            product.BasicAmounts.Clear();

            string[] s = items.GroupBy(p => p).Select(p2 => p2.Key).ToArray<string>();

            foreach (string v in s)
            {
                if (!string.IsNullOrEmpty(v))
                {
                    IBasicAmount ba = CreateProperty(v);
                    ba.Owner = product;
                    product.BasicAmounts.Add(ba);
                }
            }
        }

        public override string GetItemByPolicy(IPolicy policy)
        {
            IBasicAmount ba = policy.SelectedBasicAmount;

            policy.SetSettingLock(Owner.Name, policy.Product.BasicAmounts.Count > 1 ? false : true);

            if (ba != null)
                return ba.Name;
            else
                return null;

        }

        public override  bool IsLadder { get { return Owner.Name=="阶梯保额";} }

        public  UnitType UnitType { get; set; }

        public override bool IsToPropertiesEnable
        {
            get { return true; }
        }

        public override void ToProperties()
        {
            CreateProperties(Owner.Owner, Owner.Items.Split(';'));

            if (Owner.Name == "份数" || Owner.Name == "份")
            {
                Owner.Owner.UnitType = UnitType.Qty;
            }
            else
            {
                Owner.Owner.UnitType = UnitType.Amt;
            }

            Owner.Owner.IsBasicAmountsFromFixRate = true;
        }

        public override bool IsPolicySelectedEnable
        {
            get { return true; }
        }

        public override void ToPolicySelected(IPolicy policy, string item)
        {
            IBasicAmount ba = policy.Product.BasicAmounts.FirstOrDefault(p => p.Name == item);

            if (ba != null)
                policy.SelectedBasicAmount = ba;
            else
                throw new Exception(string.Format("无法将{0}转换到基本保额",item));
        }

        public override bool IsDisplay
        {
            get
            {
                return !IsLadder;
            }
        }

    }
}
